//
//  ZWVistorView.swift
//  张伟的微博
//
//  Created by 伟 on 16/2/27.
//  Copyright © 2016年 伟. All rights reserved.
//

import UIKit
//1 定义代理
protocol ZWVistorDelegate: NSObjectProtocol {
    func vistorViewRegisterButtonClick ()
    func vistorViewLoadButtonClick ()
}

class ZWVistorView: UIView {
    
    //2 申明代理属性
    weak var vistorViewDelegate : ZWVistorDelegate?
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        self.backgroundColor = UIColor(white: 235/255.0, alpha: 1)
        preparaUI()
        
        
        
    }
    //从xib/storboyd加载时会走这个方法
    //重载init(frame: CGRect)方法后就不会走这个方法
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    // MARK: - 设置约束
    
    func preparaUI() {
    
        //添加图像
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loadButton)
        
        
        
        //关闭自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loadButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        //转轮的X
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //转轮的Y
      addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //房子的x
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
         addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //laber x = 房子的x
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度为240
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        
        //注册与label左边对齐
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //距离label16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //宽度100，高度35
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        
        //登录与label右边对齐
        addConstraint(NSLayoutConstraint(item: loadButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //距离label16
        addConstraint(NSLayoutConstraint(item: loadButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //宽度100，高度35
        addConstraint(NSLayoutConstraint(item: loadButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loadButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        //布局遮盖
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
        
        
    }
    // MARK: - 设置每个控制器的图片
    func setVistorView(imageName: String,msgLabel: String) {
        //隐藏房子
        homeView.hidden = true
        
        iconView.image = UIImage(named: imageName)
        
        messageLabel.text = msgLabel
        //将姿势图放入最底层
        sendSubviewToBack(coverView)
    
    }
    
    //转盘转动
    func iconViewRevolve() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        //转动的角度
        anim.duration = 20
            //旋转完不取消
        anim.removedOnCompletion = false
        //将它添加到view
        iconView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    
    
    // MARK: - 懒加载
    //转盘
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

    lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text = "关注一些人,看看有上面惊喜"
        label.font = UIFont.systemFontOfSize(18)
        label.textColor = UIColor.blackColor()
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        return label
    }()
    
    lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击方法
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    return button
    }()
    lazy var loadButton: UIButton = {
        let button = UIButton()
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: "loadButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    // 3 调用代理方法
    func registerButtonClick () {
        vistorViewDelegate?.vistorViewRegisterButtonClick()
    }
    func loadButtonClick () {
        vistorViewDelegate?.vistorViewLoadButtonClick()
    
    }
    //遮盖
    lazy var coverView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}
